/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tools;

import interfaces.GameState;

/**
 *
 * @author Bruno
 */
public class StateReader {
    private int[]   state;
    private int     width, height;
    private int     lines;
    
    public StateReader(GameState gs) {
        state = gs._getState();
        
        width   = gs.getSize().width;
        height  = gs.getSize().height;
        
        lines = width * (height + 1) + height * (width + 1);
    }
    
    public int getNumberOfLines() {
        return lines;
    }
    
    public int isLineSet(int index) {
        if(index >= 0 && index < lines) {
            int bit, x, y, result;
            
            if(index < (width * 2 + 1) * height) {
                x = (index % (width * 2 + 1)) / 2;
                y = index / (width * 2 + 1);
                
                if(x > 0 && x % width == 0) {
                    bit = 4;
                } else if(index % 2 != 0) {
                    if(y % 2 == 0)
                        bit = 4;
                    else
                        bit = 8;
                } else {
                    if(y % 2 == 0)
                        bit = 8;
                    else
                        bit = 4;
                }
            } else {
                bit = 8;
                
                x = index - ((width * 2 + 1) * height);
                y = height;
            }
            
            return (state[y] & (bit << (4 * x))) == 0 ? 0 : 1;
        } else {
            return -1;
        }
    }
}
